<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<script>
    function getShiCha(t1,t2){
        var f1 = false;
        var f2 = false;
        if(!t1 instanceof Date){
            f1 = true;
        }
        if(!t2 instanceof Date){
            f2 = true;
        }
        if(f1){
            alert("参数t1不是时间类型");
            return ;
        }else{
            if(f2){
                alert("参数t2不是时间类型");
                return;
            }else{
                var m1 = t1.getTime();
                var m2 = t2.getTime();
                //获取毫秒值之差的绝对值
                var cha = m1 > m2 ? (m1-m2) : (m2 - m1);
                //获取天数毫秒值
                var day = parseInt(cha/(1000*60*60*24));
                cha -= day * (1000*60*60*24);
                var hour = parseInt(cha/(1000*60*60));
                cha -= hour * (1000*60*60);
                var minute = parseInt(cha/(1000*60));
                cha -= minute * (1000*60);
                var mile = parseInt(cha/1000);
                alert("两个日期相差"+day+"天"+hour+"小时"+minute+"分钟"+mile+"秒");
            }
        }
    };

    var d1 = new Date("2020-06-22 22:52:00");
    var d2 = new Date("1994-12-19 06:08:16");
    getShiCha(d1,d2);
</script>
</body>

</html>